package LawsonTransformJava.pgm;

import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import LawsonTransformJava.xref.CrossRef;
import LawsonTransformJava.xref.XrefPosition;

import LawsonTransformJava.dao.DAODependent;
import LawsonTransformJava.dao.DAOFactoryAccessDB;
import LawsonTransformJava.dao.DAOFactoryOracleDB;
import LawsonTransformJava.dao.DAObject;
import LawsonTransformJava.data.TblDependent;

public class HR513 extends DAObject implements 
DAODependent, Runnable {
	
	public HR513(File output, File errOutput, File errReport) {
		try {
			//Setup the various output files and reports
			this.output = new FileWriter(this.outputFile = output);
			this.errOutput = new FileWriter(this.errOutputFile = errOutput);
			this.errReport = new Report(errReport, new Date());

			// initialization
			init();
		} catch (IOException e) {
			// Could no write properties file
			System.out.println("IOException: " + e);
		}
	}

	private void init() throws IOException {
		// Set the data variables
		data = DAOFactoryOracleDB.getDAOOracleDB(true);

		errReport.setPgmName(this.getClass().getSimpleName());

		sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME;
		stmtData = data.getDataStatement();	    
		countStmtData = data.getCountStatement();
	}

	public TblDependent findDependent() {
		try {
			TblDependent dep = new TblDependent();

			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause);
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
			recCount =  this.countDependents();
			if (rslt.getRow()==recCount)
				hasMoreRecs = false;
			return (TblDependent) createObject(rslt, dep);
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return null;
	}

	public TblDependent findDependent(String employeeNumber) {
		TblDependent dep = new TblDependent();
		try {		
			sqlData = "SELECT * FROM " + data.getSchema() + "." + TABLE_NAME + " WHERE " + TABLE_NAME + "='" + employeeNumber + "'";
			rslt = data.getDataStatement().executeQuery(sqlData);
			rsmd = rslt.getMetaData();
			rslt.next();
			return (TblDependent) createObject(rslt, dep);
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return new TblDependent();
	}

	public TblDependent nextDependent() {
		TblDependent dep = new TblDependent();

		try {
			if (rslt.next()) {
				hasMoreRecs = true;
				dep = (TblDependent) createObject(rslt, dep);
			} else
				hasMoreRecs = false;
			return dep;
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		} catch (InvocationTargetException ite) {
			System.out.println("InvocationException: " + ite);
		} catch (IllegalAccessException iae) {
			System.out.println("IllegalAccessException: " + iae);
		}
		return null;
	}

	public boolean hasMore () {
		return hasMoreRecs;
	}

	public int countDependents() {
		try {
			countRecs = data.getCountStatement().executeQuery("SELECT COUNT(*) FROM " + data.getSchema() + "." + TABLE_NAME + (whereClause==null?"":whereClause));
			countRecs.next();
			return recCount=countRecs.getInt(1);
		} catch (SQLException e) {
			// Could not connect to the database
			System.out.println("SQLException: " + e);
		}
		return 0;
	}

	public void setWhereClause(String where) {
		this.whereClause = where;
	}

	public void run() {
		TblDependent dep = null;

		try {
			//Update current date/time
			System.out.println(errReport.getName() + " Start  " + Report.getDateFormat().format(new Date()));

			//@ TODO DEBUG - verify this line needs to exist
			System.out.println(whereClause + " " + countDependents());
			dep = findDependent();
			while (hasMore()) {
				writeHR513Layout(dep);
				dep = nextDependent();
			}
			//Update current date/time
			System.out.println(errReport.getName() + " Finish " + Report.getDateFormat().format(new Date()));

			//Write Record Counts
			errReport.writeRecordCounts(this.countDependents());
			
			//Reclaim Resources
			fileCleanup();

		} catch (IOException e) {
			// Could not write properties file
			// Could not create output file
			System.out.println("IOException: " + e);
		}
	}

	//Output the HR513 format
	public void writeHR513Layout(TblDependent dep) throws IOException {

		String writeValue = null;

		writeValue =
				"A" + "," +
				"1" + "," +
				//@ TODO CODE - Update to consolidate dup EEs
				dep.getEmployee() + "," +
				dep.getSeq_nbr() + "," +
				"\"" + dep.getLast_name() + "\"" + "," +
				"\"" + dep.getFirst_name() + "\"" + "," +
				"\"" + dep.getMiddle_init() + "\"" + "," +
				"\"" + dep.getFica_nbr() + "\"" + "," +
				"\"" + dep.getEmp_address() + "\"" + "," +
				"\"" + dep.getRel_code() + "\"" + "," +
				"\"" + dep.getDep_type() + "\"" + "," +
				dep.getBirthdate() + "," +
				"\"" + dep.getSex() + "\"" + "," +
				"\"" + dep.getSmoker() + "\"" + "," +
				"\"" + dep.getStudent() + "\"" + "," +
				"\"" + dep.getDisabled() + "\"" + "," +
				"\"" + dep.getPrimary_care() + "\"" + "," +
				"\"" + dep.getHl_cov_flag() + "\"" + "," +
				"\"" + dep.getDn_cov_flag() + "\"" + "," +
				"\"" + dep.getDl_cov_flag() + "\"" + "," +
				"\"" + dep.getAddr1() + "\"" + "," +
				"\"" + dep.getAddr2() + "\"" + "," +
				"\"" + dep.getAddr3() + "\"" + "," +
				"\"" + dep.getAddr4() + "\"" + "," +
				"\"" + dep.getCity() + "\"" + "," +
				"\"" + dep.getState() + "\"" + "," +
				"\"" + dep.getZip() + "\"" + "," +
				"\"" + dep.getCountry_code() + "\"" + "," +
				"\"" + dep.getHm_phone_cntry() + "\"" + "," +
				"\"" + dep.getHm_phone_nbr() + "\"" + "," +
				"\"" + dep.getWk_phone_cntry() + "\"" + "," +
				"\"" + dep.getWk_phone_nbr() + "\"" + "," +
				"\"" + dep.getWk_phone_ext() + "\"" + "," +
				dep.getAdoption_date() + "," +
				dep.getPlacement_date() + "," +
				"\"" + dep.getConsent() + "\"" + "," +
				"\"" + dep.getActive_flag() + "\"" + "," +
				"\"" + dep.getLast_name_pre() + "\"" + "," +
				"\"" + dep.getName_suffix() + "\"" + "," +
				"\"" + dep.getDeceased() + "\"" + "," +
				dep.getDeath_date() + "," +
				dep.getPrior_cov_mo() + "," +
				dep.getEstab_patient() + "," +
				"\"" + dep.getMedicare_ind() + "\"" + "," +
				"," +
				","			 
			;
		
		if (CrossRef.isError()) {
			//Increment Record in Error
			errReport.addErrorCnt();
			//Write to Error File
			this.errOutput.write(writeValue + "\n");
			this.errOutput.flush();
			CrossRef.resetError();
		}
		else {
			//Increment Record Written
			errReport.addWrittenCnt();
			//Write to Load File
			this.output.write(writeValue + "\n");
			this.output.flush();
		}		
	}
	
	public void fileCleanup() throws IOException {
		errReport.flush();
		errReport.close();
		output.flush();
		output.close();
		errOutput.flush();
		errOutput.close();
	}
}